package com.watt.gateway.interceptor;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.watt.gateway.domain.FailResultEntity;
import com.watt.gateway.domain.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 *
 */
@Component
public class CustomZuulErrorFilter extends ZuulFilter {

    private static Logger logger = LoggerFactory.getLogger(CustomZuulErrorFilter.class);

    @Override
    public String filterType() {
        //异常过滤器
        return "error";
    }

    @Override
    public int filterOrder() {
        //优先级，数字越大，优先级越低
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        //是否执行该过滤器，true代表需要过滤
        return true;
    }

    @Override
    public ResultEntity run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        logger.error("拦截到异常:", ctx.getResponseBody());
        ctx.setResponseBody("出现异常");
        return new FailResultEntity(ctx.getResponseBody());
    }
}

